﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace LMIS_Application
{
    public class Member
    {
        private int memberId;   //not nullable
        private string name;    //not nullable
        private long phone;      //not nullable     
        private string address; //not nullable
        private string status; //not nullable
        private int groupId;
        private float accountBalance;

        public int? MemberID
        {
            get
            {
                return memberId;
            }
            private set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("MemberID value cannot be null!");
                }

                memberId = (int)value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("Name value cannot be null!");
                }
                name = value;
            }
        }

        public long? Phone
        {
            get
            {
                return phone;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("Phone value cannot be null!");
                }
                phone = (long)value;
            }
        }

        public string Address
        {
            get
            {
                return address;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("Address value cannot be null!");
                }
                address = value;
            }
        }

        public string Status
        {
            get
            {
                return status;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("Status value cannot be null!");
                }
                status = value;
            }
        }

        public Member()
        {
            //Set the default memberId to -1. In SaveMember, if the memberId is -1 then we know we'll have to INSERT, otherwise UPDATE, this record.
            this.memberId = -1;
        }

        public void LoadMember(int memberId)
        {
            string command = "SELECT * from Member WHERE MemberId='" + memberId + "'";

            using (SqlConnection conn = (SqlConnection)DbHelper.GetDatabaseConnection())
            {
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(command, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Member");

                int rowCount = ds.Tables["Member"].Rows.Count;

                if (rowCount > 0)
                {
                    //get the first row
                    DataRow dr = ds.Tables["Member"].Rows[0];
                    this.MemberID = (int)dr["MemberId"];
                    this.Name = (string)dr["Name"];
                    this.Phone = Convert.ToInt64(dr["Phone"]);
                    this.Address = (string)dr["Address"];
                    this.Status = (string)dr["Status"];
                }
            }
        }

        public bool SaveMember()
        {
            string command = null;
            bool success = false;

            if (this.memberId == -1)
            {
                command = "INSERT into Member (Name, Phone, Address, Status) values ('"+this.Name+"', '"+this.Phone.ToString()+"', '"+this.Address+"', '"+this.Status+"')"; //INSERT
            }
            else
            {
                command = "UPDATE Member SET Name='"+this.Name+"', Phone='"+this.Phone.ToString()+"', Address='"+this.Address+"', Status='"+this.Status+"' WHERE MemberId='"+this.MemberID+"'"; //UPDATE
            }

            using (SqlConnection conn = (SqlConnection)DbHelper.GetDatabaseConnection())
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(command, conn);
                int rowsAffected = cmd.ExecuteNonQuery();
                success = rowsAffected > 0;
            }

            return success;
        }

        public static bool DeleteMember(int mid)
        {
            string command = null;
            bool success = false;
            if (mid == -1)
            {
                throw new InvalidOperationException("Cannot delete a member who has not yet been added.");
            }
            command = "DELETE from Member where MemberId='" + mid + "'";
            using (SqlConnection conn = (SqlConnection)DbHelper.GetDatabaseConnection())
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(command, conn);
                int rowsAffected = cmd.ExecuteNonQuery();
                success = rowsAffected > 0;
            }
            return success;
        }

        public static List<Member> GetMemberList()
        {
            List<Member> members = new List<Member>();
            string command = "SELECT MemberId from Member";

            using (SqlConnection conn = (SqlConnection)DbHelper.GetDatabaseConnection())
            {
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(command, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Member");

                foreach (DataRow r in ds.Tables["Member"].Rows)
                {
                    int memId = (int)r["MemberId"];
                    Member m = new Member();
                    m.LoadMember(memId);
                    members.Add(m);
                }
            }

            return members;
        }
    }
}
